/*
 * MainJFrame.java
 *
 * Created on 27 de noviembre de 2007, 16:36
 */
package bts.view;

import bts.util.DMesg;
import javax.swing.JOptionPane;
import bts.back.BtsTerm;

/**
 *
 * @author  juan
 */
public class MainJFrame extends javax.swing.JFrame {

    private static MainJFrame INSTANCE = null;
    private bts.view.TermJPanel at;

    public static MainJFrame getInstance() {
        if (INSTANCE == null) createInstance();
        return INSTANCE;
    }

    private synchronized static void createInstance() {
        if (INSTANCE == null) INSTANCE = new MainJFrame();
    }

    /**
     * Creates new form MainJFrame
     */
    private MainJFrame() {
        initComponents();
        DMesg.setJLabelOuputAndError(this.jLabel6);
        DMesg.outln("BTS OK!");
    }

    public void setTabTittle(java.awt.Component component, String title) {
        if (title.length() > 10) {
            title = title.substring(0, 10) + "...";
        }
        this.mainJTabbedPane.setTitleAt(this.mainJTabbedPane.indexOfComponent(component), title);
    }

    private void chargeTerm(BtsTerm term) {
        this.at = new bts.view.TermJPanel(term);
        mainJTabbedPane.addTab("", at);
        this.mainJTabbedPane.setSelectedComponent(at);
        this.setTabTittle(at, at.getTermName());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel8 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        addTermJButton = new javax.swing.JButton();
        searchTermJButton = new javax.swing.JButton();
        searchTermJButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jButton1 = new javax.swing.JButton();
        mainJTabbedPane = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem11 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(bts.var.Info.PROGRAM_NAME + " - " + bts.var.Info.VERSION + " Version");

        jPanel8.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel8.setMaximumSize(new java.awt.Dimension(0, 0));
        jPanel8.setMinimumSize(new java.awt.Dimension(100, 15));
        jPanel8.setPreferredSize(new java.awt.Dimension(100, 15));
        jPanel8.setLayout(new java.awt.GridLayout(1, 2, 5, 0));

        jLabel5.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel5.setText("User: John Doe");
        jPanel8.add(jLabel5);

        jLabel6.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Task Info");
        jPanel8.add(jLabel6);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        addTermJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bts/front/img/icons/add.png"))); // NOI18N
        addTermJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTermJButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(addTermJButton);

        searchTermJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bts/front/img/icons/search.png"))); // NOI18N
        searchTermJButton.setEnabled(false);
        jToolBar1.add(searchTermJButton);

        searchTermJButton1.setText("Save");
        searchTermJButton1.setEnabled(false);
        searchTermJButton1.setFocusable(false);
        searchTermJButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        searchTermJButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(searchTermJButton1);
        jToolBar1.add(jSeparator1);

        jButton1.setText("Delete Term");
        jButton1.setEnabled(false);
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        mainJTabbedPane.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
        mainJTabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                mainJTabbedPaneStateChanged(evt);
            }
        });

        jMenu1.setMnemonic('A');
        jMenu1.setText("Archivo");

        jMenuItem5.setText("Nuevo...");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);

        jMenuItem6.setText("Buscar...");
        jMenuItem6.setEnabled(false);
        jMenu1.add(jMenuItem6);

        jMenuItem8.setText("Guardar...");
        jMenuItem8.setEnabled(false);
        jMenu1.add(jMenuItem8);

        jMenuItem10.setText("Cerrar");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem10);

        jMenuItem7.setText("Borrar...");
        jMenuItem7.setEnabled(false);
        jMenu1.add(jMenuItem7);

        jSeparator2.setEnabled(false);
        jMenu1.add(jSeparator2);

        jMenuItem9.setText("Salir");
        jMenuItem9.setEnabled(false);
        jMenu1.add(jMenuItem9);

        jMenuBar1.add(jMenu1);

        jMenu4.setText("Lenguajes");

        jMenuItem11.setText("Administrar Lenguajes");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem11);

        jMenuBar1.add(jMenu4);

        jMenu2.setText("Herramientas");

        jMenuItem3.setText("Exportar");
        jMenuItem3.setEnabled(false);
        jMenu2.add(jMenuItem3);

        jMenuItem4.setText("Traducción Automática");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem4);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("Ayuda");

        jMenuItem1.setMnemonic('C');
        jMenuItem1.setText("Contenido");
        jMenuItem1.setEnabled(false);
        jMenu3.add(jMenuItem1);

        jMenuItem2.setText("Acerca de BTS...");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem2);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 778, Short.MAX_VALUE)
            .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, 778, Short.MAX_VALUE)
            .addComponent(mainJTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 778, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mainJTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 669, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-790)/2, (screenSize.height-799)/2, 790, 799);
    }// </editor-fold>//GEN-END:initComponents
    private void mainJTabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_mainJTabbedPaneStateChanged
        at = (bts.view.TermJPanel) this.mainJTabbedPane.getSelectedComponent();
    }//GEN-LAST:event_mainJTabbedPaneStateChanged

    private void addTermJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addTermJButtonActionPerformed
        this.jMenuItem5ActionPerformed(evt);
    }//GEN-LAST:event_addTermJButtonActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        String name = JOptionPane.showInputDialog(null, "Ingrese el nombre del nuevo término", "Nuevo Término", JOptionPane.QUESTION_MESSAGE);
        if (name != null) {
            bts.back.BtsTerm term = new bts.back.BtsTerm(name);
            this.chargeTerm(term);
        }
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        bts.view.help.AboutJDialog.open();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        BtsTerm aux = null;
        try {
            aux = at.getTerm();
        } catch (Exception e) {
        }
        aux = bts_google_translate_tool.view.Translate_TermJDialog.open(aux);
        if (aux != null) {
            this.chargeTerm(aux);
        }
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
        int aux = this.mainJTabbedPane.getSelectedIndex();
        this.mainJTabbedPane.remove(aux);
    }//GEN-LAST:event_jMenuItem10ActionPerformed

    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem11ActionPerformed
       // bts.front.languages.ManageLanguageJDialog.open();
    }//GEN-LAST:event_jMenuItem11ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainJFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addTermJButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTabbedPane mainJTabbedPane;
    private javax.swing.JButton searchTermJButton;
    private javax.swing.JButton searchTermJButton1;
    // End of variables declaration//GEN-END:variables
}
